package com.example.administrator.videotest2;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Spinner;
import com.example.administrator.videotest2.config.VideoApi;
import com.example.administrator.videotest2.entity.TextEntity;
import com.example.administrator.videotest2.util.To_playerAdapter;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class ToPlayer_Activity extends AppCompatActivity {
    private String xl_url1= VideoApi.jk1;
    private String xl_url2=VideoApi.jk2;
    private String xl_url3=VideoApi.jk3;
    private String xl_url4=VideoApi.jk4;
    /*************/
    private String xl_url5=VideoApi.jiexi_5;
    private String xl_url6=VideoApi.jiexi_6;
    private String xl_url7=VideoApi.jiexi_7;
    private String xl_url8=VideoApi.jiexi_8;
    private String xl_url9=VideoApi.jiexi_9;
    private String xl_url10=VideoApi.jiexi_10;
    private String xl_url="";

    List<TextEntity> datalist;
    private List<String> xl_list;
    private ArrayAdapter<String> arr_adapter;
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_player_);
        spinner = (Spinner) findViewById(R.id.spinner);
        String url=getIntent().getStringExtra("url");
        datalist=initList(url);
        xl_list = new ArrayList<String>();
        xl_list.add("线路1【推荐】");
        xl_list.add("线路2");
        xl_list.add("线路3");
        xl_list.add("线路4");
     /*   xl_list.add("线路4【推荐】");
        xl_list.add("线路5【推荐】");
        xl_list.add("线路6");
        xl_list.add("线路7");
        xl_list.add("线路8");
        xl_list.add("线路9");
        xl_list.add("线路10");
        xl_list.add("线路11");*/
        //适配器
        arr_adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, xl_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner.setAdapter(arr_adapter);
        spinner.setSelection(0,true);

        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
               if(arg2==0){
                   xl_url=xl_url4;
               }
                else if(arg2==1)
               xl_url=xl_url1;
               else if(arg2==2)
                   xl_url=xl_url2;
               else if(arg2==3)
                   xl_url=xl_url3;
               else
                   xl_url=VideoApi.jk4;

            }
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });


       this.runOnUiThread(new Runnable() {
           @Override
           public void run() {

               To_playerAdapter to_playerAdapter = new To_playerAdapter(ToPlayer_Activity.this, R.layout.toplayer_item, datalist);
               GridView listView = (GridView)findViewById(R.id.to_player_view);
               listView.setAdapter(to_playerAdapter);
               SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ToPlayer_Activity.this);
               listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                       TextEntity item = (TextEntity) parent.getItemAtPosition(position);
                       //Log.i("we23" ,cover.getImage().toString());
                       //h1.get()
                       // String image=view.getIm
                       Intent intent = new Intent(ToPlayer_Activity.this,PlayerActivity.class);
                       intent.putExtra("url", item.getPlurl().toString());
                       intent.putExtra("xl_url",xl_url);
                       startActivity(intent);
                   }
               });

           }
       });

    }


    public List<TextEntity> initList(String url)
    {
        List<TextEntity> datalist=new ArrayList<>();
        if(url.contains(",")) {
            String[] array = url.split(",");
            for(int i=1;i<=array.length;i++){
                if(array[i-1].contains("#")){
                    continue;
                }
                TextEntity item=new TextEntity();
                item.setNum("第"+i+"集");
                Log.i("purl",array[i-1]);
                item.setPlurl(array[i-1]);
                datalist.add(item);
            }
        }
        else {
            TextEntity item=new TextEntity();
            item.setNum("高清");
            item.setPlurl(url);
            datalist.add(item);
        }
        return datalist;
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


}
